package com.tal.pad.eye_protection.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

public class AssetsUtil {
    private static final String TAG = "AssetsUtil";



    public static Bitmap getBitmapFromAsset(Context context, String imageFileName,String dirName) {
        try {
//            InputStream inputStream = context.getAssets().open("test_imgs/" + imageFileName);
            InputStream inputStream = context.getAssets().open(dirName+"/" + imageFileName);
            return BitmapFactory.decodeStream(inputStream);
        } catch (IOException e) {
            Log.e(TAG, "Failed to load image from asset: " + e.getMessage());
            return null;
        }
    }

    /**
     * 将 Bitmap 转换为 YUV420 格式的字节数组
     *
     * @param bitmap 输入的 Bitmap 对象
     * @return YUV420 格式的字节数组
     */
    public static byte[] convertBitmapToYuv420(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int yuvSize = width * height * 3 / 2;
        byte[] yuv = new byte[yuvSize];

        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        int yIndex = 0;
        int uvIndex = width * height;

        int R, G, B, Y, U, V;
        int index = 0;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int argb = pixels[index++];
                R = (argb >> 16) & 0xff;
                G = (argb >> 8) & 0xff;
                B = argb & 0xff;

                // 计算 YUV 值
                Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
                U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
                V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;

                Y = Math.max(0, Math.min(Y, 255));
                U = Math.max(0, Math.min(U, 255));
                V = Math.max(0, Math.min(V, 255));

                yuv[yIndex++] = (byte) Y;
                if (y % 2 == 0 && x % 2 == 0) {
                    yuv[uvIndex++] = (byte) U;
                    yuv[uvIndex++] = (byte) V;
                }
            }
        }
        return yuv;
    }
    public static byte[] convertBitmapToNV21(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] argb = new int[width * height];
        // 获取 Bitmap 的像素数据
        bitmap.getPixels(argb, 0, width, 0, 0, width, height);

        // 计算 NV21 数据所需的字节数
        byte[] yuv = new byte[width * height * 3 / 2];
        final int frameSize = width * height;
        int yIndex = 0;
        int uvIndex = frameSize;

        int a, R, G, B, Y, U, V;
        int index = 0;
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {
                // 提取 ARGB 分量
                a = (argb[index] & 0xff000000) >> 24; // 忽略 alpha 值
                R = (argb[index] & 0xff0000) >> 16;
                G = (argb[index] & 0xff00) >> 8;
                B = (argb[index] & 0xff);

                // 计算 Y 分量
                Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
                Y = Math.max(0, Math.min(Y, 255));

                yuv[yIndex++] = (byte) Y;

                if (j % 2 == 0 && i % 2 == 0) {
                    // 计算 U 和 V 分量
                    U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
                    V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
                    U = Math.max(0, Math.min(U, 255));
                    V = Math.max(0, Math.min(V, 255));

                    yuv[uvIndex++] = (byte) V;
                    yuv[uvIndex++] = (byte) U;
                }
                index++;
            }
        }
        return yuv;
    }

    /**
     * 获取assets文件夹下的 指定文件夹的全部文件名称
     * @param context
     * @return
     */
    public static String[] getFilesFromAssets(Context context,String path) {
        try {
            // 获取 AssetManager 实例
            return context.getAssets().list(path);
        } catch (IOException e) {
            e.printStackTrace();
            return new String[0];
        }
    }
    private String[] names=null;
}
